package com.pkx.guide.file.controller;

import com.pkx.guide.common.annotation.ORpose;
import com.pkx.guide.common.result.Result;
import com.pkx.guide.file.service.SysFileService;
import com.pkx.guide.system.model.dto.FileInfo;
import com.pkx.guide.file.service.OssService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "文件接口")
@RestController
@RequestMapping("/file")
@ORpose
public class FileController {

    @Resource
    private OssService ossService;

    @Resource
    private SysFileService sysFileService;

    @PostMapping
    @Operation(summary = "文件上传")
    public FileInfo uploadFile(@Parameter(description ="表单文件对象") @RequestParam(value = "file") MultipartFile file) {
        return ossService.uploadFile(file);
    }

    @Operation(summary = "文件上传请求")
    @PostMapping("/upload")
    public FileInfo upload(@Parameter(description ="表单文件对象") @RequestParam(value = "file") MultipartFile file) throws Exception {
        // 上传并返回访问地址
        FileInfo fileInfo = sysFileService.uploadFile(file);
        String fileName = file.getOriginalFilename();
        fileInfo.setOriginName(fileName);
        return fileInfo;
    }

    @DeleteMapping
    @Operation(summary = "文件删除")
    @SneakyThrows
    public Boolean deleteFile(@Parameter(description ="文件路径") @RequestParam String filePath) {
        return ossService.deleteFile(filePath);
    }
}
